package com.example.userlist.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.userlist.dto.UserDto;
import com.example.userlist.entity.User;
import com.example.userlist.service.UserService;
import com.example.userlist.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    @GetMapping("/index")
    public String index(){
        return "hello";
    }

    @PostMapping("/register")
    public Result<String> addUser(@RequestBody UserDto userDto) {
        log.info(userDto.toString());
        User user = new User();
        BeanUtils.copyProperties(userDto,user);
        user.setHobby(StringUtils.arrayToCommaDelimitedString(userDto.getHobby()));
        log.info(user.toString());
        boolean save = userService.save(user);
        return Result.success("插入:" + save);
    }


    @GetMapping
    public List<User> getUser(UserDto userDto) {
//        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        wrapper.like("username",userDto.getUsername());
        List<User> list = userService.list();
        return list;
    }



}
